﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CoordinateInputEditor.h"
#include "GbmpNew.h"
#include "QStyleFactory"
#include "LineEditValidateHelper.h"
#include "ValidatorBase.h"
#include "QtLineEdit.h"
#include <QAction>
#include <QFontMetrics>
#include <QList>
#include <QWidget>
#include "CoordinateInputDefinition.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

gcmp::CoordinateInputEditor::CoordinateInputEditor(OwnerPtr<CoordinateInputDefinition> opControlDefinition, QWidget *pParent)
    : QWidget(pParent)
    , ICoordinateInputEventHandler(TransferOwnership(opControlDefinition))
    , m_isLock(false)
    , m_isEditing(false)
    , m_minWidth(60)
    , m_maxWidth(95)
    , m_minHeight(22)
{
    m_lineEdit = NEW_AS_OWNER_PTR(QtLineEdit, TransferOwnership(GetDefinitionImpl()), pParent);
    m_opDefinition->SetCoordinateInputEventHandler(this);

    // 大小设置
    m_lineEdit->setFixedSize(m_minWidth, m_minHeight);

    // 默认样式设置
    m_lineEdit->setStyleSheet(
        "border - width: 1px;"
        "border - color: #343434;"
        "color: #AAAAAA");

    // 编辑状态和非编辑状态样式设置
    QObject::connect(m_lineEdit.get(), &QLineEdit::textEdited, this, &CoordinateInputEditor::OnEditingText);
    QObject::connect(m_lineEdit.get(), &QLineEdit::editingFinished, this, &CoordinateInputEditor::OnEditFinished);
    QObject::connect(m_lineEdit.get(), &QLineEdit::textChanged, this, &CoordinateInputEditor::OnTextChanged);

    QObject::connect(m_lineEdit.get(), &QLineEdit::returnPressed, this, &CoordinateInputEditor::returnPressed);

    // 防止全局Style影响
    m_lineEdit->setStyle(QStyleFactory::create("WindowsVista"));

    // 设置锁样式
    m_pLockAction = NEW_AS_QT(QAction, this);
    m_pLockAction->setIcon(QIcon(":/images/lock.png"));

    //m_lineEdit->setValidator(nullptr);// Definition自带限制不通用

    FitDefinitin();
}

gcmp::CoordinateInputEditor::~CoordinateInputEditor()
{
}

void gcmp::CoordinateInputEditor::On(CoordinateInputEventArgs * pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs为空",L"GDMPLab",L"2024-03-30");
    CoordinateInputEventType type = pArgs->GetType();
    if (CoordinateInputEventType::LockStateChange == type)
    {
        SetLock(pArgs->IsLock());
    }
    //else if (CoordinateInputEventType::SetStyleSheet == type)
    //{
    //    m_lineEdit->setStyleSheet(QString::fromStdWString(pArgs->GetStyleSheet()));
    //}
}

bool gcmp::CoordinateInputEditor::FitDefinitin()
{
    SetLock(m_opDefinition->IsLock());
    return true;
}

bool gcmp::CoordinateInputEditor::AdjustSize()
{
    QString text = m_lineEdit->text();
    QFontMetrics fm = m_lineEdit->fontMetrics();

    int actionWidgetSize = 0;
    if (m_isLock)
    {
        actionWidgetSize = 2;
    }
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pLockAction->associatedWidgets().size() == actionWidgetSize, L"锁Action的Widget数量不对",L"GDMPLab",L"2024-03-30");

    int width = fm.width(text);
    if (m_isLock)
    {
        QWidget *pWidget = m_pLockAction->associatedWidgets()[1];
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pWidget, L"m_pLockAction相关的widget*怎么可能是空的？",L"GDMPLab",L"2024-03-30");

        int margin = m_minHeight > 34 ? 4 : 8;// 基于qt实现，详情见QLineEditPrivate::sideWidgetParameters
        width += pWidget->width() + 2 * margin;
    }
    else
    {
        width += fm.width("  ");// 文本与输入框之间保留一个光标位置以及一个空格的间隙
    }

    if (width < m_minWidth)
    {
        width = m_minWidth;
    }
    if (width > m_maxWidth)
    {
        width = m_maxWidth;
    }

    m_lineEdit->setFixedWidth(width);

    return true;
}

void CoordinateInputEditor::OnEditingText()
{
    if (!m_isEditing)
    {
        m_isEditing = true;
        // 编辑状态样式设置
        m_lineEdit->setStyleSheet("border - color: #0078D7");
    }
}

void CoordinateInputEditor::OnEditFinished()
{
    m_isEditing = false;
    // 非编辑状态样式设置
    m_lineEdit->setStyleSheet("border - color: #343434");
}

void CoordinateInputEditor::OnTextChanged()
{
    AdjustSize();
}

QtLineEdit* CoordinateInputEditor::GetEditorWidget()
{
    return m_lineEdit.get();
}

const gcmp::QtLineEdit* gcmp::CoordinateInputEditor::GetEditorWidget() const
{
    return m_lineEdit.get();
}


bool gcmp::CoordinateInputEditor::SetLock(bool isLock)
{
    if (!m_isLock && isLock)
    {
        m_lineEdit->addAction(m_pLockAction, QLineEdit::TrailingPosition);
    }
    else if (m_isLock && !isLock)
    {
        m_lineEdit->removeAction(m_pLockAction);
    }
    m_isLock = isLock;

    AdjustSize();

    return true;
}
